<?php

namespace Database\Factories;

use App\Models\User;
use App\Models\Room;
use App\Models\Dish;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;

/**
 * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Order>
 */
class OrderFactory extends Factory
{
    /**
     * Define the model's default state.
     *
     * @return array<string, mixed>
     */
    public function definition(): array
    {
        $dishes = Dish::factory()->count(fake()->numberBetween(1, 3))->create();
        
        return [
            'order_id' => 'order_' . time() . '_' . Str::random(4),
            'room_id' => Room::factory(),
            'user_id' => User::factory(),
            'dishes' => $dishes->map(function ($dish) {
                return [
                    'dish_id' => $dish->id,
                    'name' => $dish->name,
                    'quantity' => fake()->numberBetween(1, 3),
                    'flavor' => fake()->optional(0.3)->randomElement(['微辣', '中辣', '重辣']),
                    'note' => fake()->optional(0.4)->sentence(),
                ];
            })->toArray(),
            'status' => fake()->randomElement(['pending', 'confirmed', 'cancelled', 'completed']),
            'payment_status' => fake()->randomElement(['unpaid', 'paid', 'refunded']),
            'payment_method' => fake()->optional(0.3)->randomElement(['wechat', 'alipay']),
            'payment_time' => fake()->optional(0.3)->dateTimeBetween('-1 week', 'now'),
            'notes' => fake()->optional(0.3)->sentence(),
        ];
    }

    /**
     * 待处理订单
     */
    public function pending(): static
    {
        return $this->state(fn (array $attributes) => [
            'status' => 'pending',
            'payment_status' => 'unpaid',
            'payment_method' => null,
            'payment_time' => null,
        ]);
    }

    /**
     * 已确认订单
     */
    public function confirmed(): static
    {
        return $this->state(fn (array $attributes) => [
            'status' => 'confirmed',
            'payment_status' => 'paid',
            'payment_method' => fake()->randomElement(['wechat', 'alipay']),
            'payment_time' => fake()->dateTimeBetween('-1 week', 'now'),
        ]);
    }

    /**
     * 已取消订单
     */
    public function cancelled(): static
    {
        return $this->state(fn (array $attributes) => [
            'status' => 'cancelled',
            'payment_status' => 'unpaid',
            'payment_method' => null,
            'payment_time' => null,
        ]);
    }

    /**
     * 已完成订单
     */
    public function completed(): static
    {
        return $this->state(fn (array $attributes) => [
            'status' => 'completed',
            'payment_status' => 'paid',
            'payment_method' => fake()->randomElement(['wechat', 'alipay']),
            'payment_time' => fake()->dateTimeBetween('-1 week', 'now'),
        ]);
    }
}
